from PIL import ImageGrab
from cv2 import cv2 
import numpy as np
import pyautogui as g
import time



def mathc_img(Target, value = 0.9):
    try:
        im = np.array(ImageGrab.grab())
        img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(Target, 0)
        res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
        # print(res)
        threshold = value
        loc = np.where(res >= threshold)
        return (int(loc[1][0]), int(loc[0][0]))
    except :
        raise Exception('未匹配到图片')
w,h=mathc_img(r'C:\Users\lixuecheng\Envs\testautoui\autoui\pic\3.png')
w0=w-313
h0=h-1
ow,oh=g.position()
print(w0,h0)
if w0 ==1 and h0 ==1:
    pass
else:
    g.moveTo(w0,h0)
    g.dragTo(1,1,duration=1)
# 滑动首页
# g.click(54,697)
# g.scroll(-1000)
# =====================
# 登陆
g.click(361,703)
time.sleep(1)
g.click(220,370)
# 授权
# g.click(188,291)
# g.click(153,455)
time.sleep(1)
g.moveTo(198,240)
g.click()
for _ in range(13):
    g.press('backspace')
g.typewrite("13800123456",interval=0.1)

g.moveTo(ow,oh)
# print()45
